const { typeJson } = require('../../utils/typejson')
const { get, post } = require('../../request/index')

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    local: {
      type: Array,
      observer: function(newVal, oldVal, changedPath) {

        const newRange = newVal.map(x => {
          return x.label
        })
        const newResult = newVal.map(x => {
          return x.value
        })
        
        this.setData({
          range: newRange,
          result: newResult
        })
     
      }
    },

    net: {
      type: Object,
      observer: function(newVal, oldVal, changedPath) {
        this.getList(newVal)
      }
    },

    value: {
      type: Number,
      observer: "handleData"
    }

  },
 

 

  /**
   * 组件的初始数据
   */
  data: {
    range: [],
    result: [],
    value: null,
    display: "",
    index:0

  },

  /**
   * 组件的方法列表
   */
  methods: {
     
    handleData:function(newVal,oldVal) {
       console.log(newVal,oldVal);
      
        var newIndex
      if (this.properties.net) {  
      } else {
        this.properties.local.map((x,y) => {
          console.log(x,newVal,y);
          if(x.value==newVal) {
            newIndex = y
          }
        })  
      }
      this.setData({
        value:newIndex,
        display: this.data.range[this.data.index],
     
      })

      
      
    
  
   
  
      
    },

    async getList(net) {
      const { url, method, key, value, data } = net
   
      if (data.mineId == true) {
        let prolist =  wx.getStorageSync('prolist')==''?wx.getStorageSync('prolist'):JSON.parse(wx.getStorageSync('prolist'))
        data.mineId = prolist.mineId
      }

      var res = ""
      if(method=='get') {
        res = await get(url, data)
      } else {
        res = await post(url, data)
      }

      
  
      let { list } = res.data
    
      var tempList = []
      var tempResult = []

      var split = []

      list.map(x=>{
        tempList.push(x[key])
        tempResult.push(x[value])
      })

      
      this.setData({
        result: tempResult,
        range: tempList 
      })
      
    },


  

    bindChange(e) {
      this.setData({
        index: e.detail.value
      })
      this.triggerEvent('change', this.data.result[this.data.index]);
    }
  }
})
